package org.btik.light.mobile.ui.serial.controller.meta;

import android.util.Log;
import org.btik.light.meta.exception.LightException;
import org.btik.light.tool.type.StringUtil;
import org.btik.light.tool.xml.DomUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import static org.btik.light.mobile.ui.serial.controller.meta.SerialDevMetaConstant.*;
import static org.btik.light.tool.xml.DomUtil.*;

/**
 * @author lustre
 * @since 2023/4/6 22:42
 */
public class SerialDevMetaFactory {
    private final static String CONTEXT_FILE_NAME = "/assets/usbVendorType.xml";

    private static final HashMap<String, String> vendorTypeMap = new HashMap<>();

    static {
        load();
    }

    private static void load() {
        try (InputStream inputStream = SerialDevMetaFactory.class.getResourceAsStream(CONTEXT_FILE_NAME)) {
            Document document = DomUtil.parse(inputStream);
            Element rootElement = document.getDocumentElement();
            if (!ROOT_TAG_NAME.equals(rootElement.getTagName())) {
                throw new LightException("root tag not match :" + rootElement.getTagName());
            }
            parse(rootElement);
        } catch (IOException | ParserConfigurationException | SAXException | LightException e) {
            e.printStackTrace();
            Log.d("error", "load: meta failed");
        }
    }

    private static void parse(Element rootElement) {
        eachByTagName(rootElement, VENDOR, (vendor) -> {
            String vendorId = vendor.getAttribute(ID);
            String vendorName = vendor.getAttribute(NAME);
            vendorTypeMap.put(vendorId, vendorName);
            eachByTagName(vendor, PRODUCT, (product) -> {
                String key = vendorId;
                String productId = product.getAttribute(ID);
                if (!StringUtil.isEmpty(productId)) {
                    key = vendorId + '/' + productId;
                }
                vendorTypeMap.put(key, product.getAttribute(TYPE));
            });
        });
    }

    /**
     * @param vendorId  厂商id 输入的为无符号16位整数
     * @param productId 厂商产品id 输入的为无符号16位整数
     * @return 厂商产品类型
     */
    public static String getVendorType(int vendorId, int productId) {
        String vendorIdStr = String.format("%04x", vendorId);
        String productIdStr = String.format("%04x", productId);
        String key = vendorIdStr + '/' + productIdStr;
        String vendorType = vendorTypeMap.get(key);

        return StringUtil.isEmpty(vendorType) ? key : vendorType;
    }

    /**
     * @param vendorId 厂商id 输入的为无符号16位整数
     * @return 厂商名称
     */
    public static String getVendorName(int vendorId) {
        String vendorIdStr = String.format("%04x", vendorId);
        String vendorName = vendorTypeMap.get(vendorIdStr);
        return StringUtil.isEmpty(vendorName) ? vendorIdStr : vendorName;
    }
}
